package com.diyunapp.xilinapp.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.selfdefine.StatusBarActivity;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by NIDAYE on 2016/7/26.
 */
public class WXPayEntryActivity extends StatusBarActivity implements IWXAPIEventHandler{

    @Bind(R.id.weixin_payresult_goback)
    ImageButton weixinPayresultGoback;
    @Bind(R.id.weixin_payprompt_iv)
    ImageView weixinPaypromptIv;
    @Bind(R.id.weixin_payprompt_tv)
    TextView weixinPaypromptTv;
    @Bind(R.id.weixinresult_confirm_bt)
    TextView weixinresultConfirmBt;

    private int payresult = 0;
    private static final String WX_APP_ID = "wx3d34176dc151d20a";
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weixin_payresult_layout);
        ButterKnife.bind(this);

        View headView = findViewById(R.id.weixinresult_menubar_inc);
        super.MenuWidget(headView);

        weixinresultConfirmBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        api = WXAPIFactory.createWXAPI(this, WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            payresult = baseResp.errCode;
            Log.d("zhangxiaoxidebug", "onPayFinish,errCode=" + payresult);
            switch (payresult)
            {
                case 0:
                    weixinPaypromptTv.setText("恭喜您，支付成功！");
                    break;
                case -1:
                    weixinPaypromptTv.setText("抱歉，支付出错了，可能原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等，请联系客服处理！");
                    break;
                case -2:
                    weixinPaypromptTv.setText("您取消了支付操作！");
                    break;
                default:
                    weixinPaypromptTv.setText("抱歉，支付未完成，发生了未知错误，错误号："+baseResp.errCode+", 请联系客服处理！");
                    break;
            }
        }
    }
}
